
var modelTitle = "";
var pager = new Pager();
$(document).ready(function () {

    pager.bind("#_page");

    //            $(".mould_right ul").each(function () {
    //                $(".mould_right ul").addClass("none");
    //                $(".mould_right ul").id = $(".mould_right ul").html();
    //            });

    //隐藏右边显示框
    $(".mould_right ul").addClass("none");

    //显示默认的右边
    $(".mould_right .active").removeClass("none");

    $(".mould_left li").bind("mouseover", function () {

        modelTitle = $.trim($(this).text());

        //                if (modelTitle == "当前模板") {
        //                    return;
        //                }
        $(".mould_left li").removeClass("active");
        $(this).addClass("active");
        $(".mould_right ul").addClass("none");

        var tbody = $(".mould_right ul[tag = '" + modelTitle + "']");
        $(tbody).removeClass("none");

        if (modelTitle != "综合") {
            $("#_page").addClass("none");
            return;
        }

        $("#_page").removeClass("none");
        if ($.trim($(tbody).html()).length != 0) {
            //如果已经加载过就不用再加载
            return;
        }

        GetList(1);
    });


    $("input[value='申请']").bind("click", function () {

        //获取图片地址
        var src = $($(this).parent().prev()).attr("src");

        var ps = src.split("/");


        //分类名
        var cls = ps[ps.length - 2];

        var fn = ps[ps.length - 1];

        //序号
        var index = fn.substring(0, fn.indexOf('.'))
        ajax.modelApply(cls, index, function (json) {
            if (json.code != 0) {
                toast.show(json.msg);
                return;
            }
            toast.show("申请成功");
        });

    });

});

function GetList(page) {

    var tbody = $(".mould_right ul[tag = '" + modelTitle + "']");

    if (wait.isShow) return;
    if (modelTitle != "综合") {
        return;
    }
    $.get("ajax/template.ashx?getlist", { page: page }, function (json) {
        tbody.empty();
        $(".pager_style").removeClass("none");
        if (json.code != 0) {
            $(tbody).append("<tr><td colspan=5>" + json.msg + "</td></tr>");
            $(".pager_style").addClass("none");
            return;
        }
        var list = json.item.Table;
        for (var i in list) {
            var src = list[i].imageurl;
            var number = list[i].id;
            number = list[i].url.substring(list[i].url.indexOf(".net.cn/") + 8, list[i].url.length - 1);
            if (number < 10)
                number = "00" + number;
            else
                number = "0" + number;
            var html = "<li>" +
                        "<div class=\"div_img\">" +
                            "<img src=\"" + src + "\" />" +
                            "<div class=\"action\"><input type=\"button\" value=\"选中\" onclick='Change(" + list[i].id + ")' /></div>" +
                        "</div>" +
                        "<div class='item_title'>编号 " + number + "</div>" +
                        "</li>";
            $(tbody).append(html);
        }
        pager.initPage(page, json.item.total, json.item.pagesize, 5, GetList);

    }, "json");
}

function Change(id) {
    if (isNaN(id))
        return;
    if (parseInt(id) < 1 || parseInt(id) > 100)
        return;

    $.post("../ajax/shop.ashx?change", { id: id }, function (json) {
        if (json.code != 0) {
            toast.show(json.msg);
            return;
        }

        toast.show("模板更新成功");

        setTimeout(function () {
            refresh();
        }, 1000);
    }, "json");
}